package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.athlete_omit;
import com.ruoyi.system.domain.athleteSkeetTargetDto;
import com.ruoyi.system.domain.vo.athleteSkeetTargetVo;
import com.ruoyi.system.domain.vo.athleteSkeetVo;
import com.ruoyi.system.service.impl.athleteSkeetServiceImpl;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/skeet")
public class athleteSkeet {

    @Resource
    athleteSkeetServiceImpl skeetService;

    @GetMapping
    public AjaxResult getSkeetScore(@Param("athleteSkeetVo") athleteSkeetVo athleteSkeetVo){
        if (athleteSkeetVo.getStartDay() == null || athleteSkeetVo.getEndDay() == null){
            LocalDate thirtyDaysAgo = LocalDate.now().minusDays(30);
            Date date = Date.from(thirtyDaysAgo.atStartOfDay(ZoneId.systemDefault()).toInstant());
            athleteSkeetVo.setStartDay(date);
            athleteSkeetVo.setEndDay(new Date());
        }
        return AjaxResult.success(skeetService.getSkeetByDate(athleteSkeetVo));
    }

    @GetMapping("/daytarget")
    public AjaxResult getDayTarget(@Param("athleteSkeetTargetVo")athleteSkeetTargetVo athleteSkeetTargetVo){
        if (athleteSkeetTargetVo.getTraindate() == null){
            athleteSkeetTargetVo.setTraindate(new Date());
        }
        System.out.println(athleteSkeetTargetVo.getTraindate());
        List<athleteSkeetTargetDto> trainingStatsByDateAndId = skeetService.getTrainingStatsByDateAndId(athleteSkeetTargetVo);
        return AjaxResult.success(trainingStatsByDateAndId);
    }

    @GetMapping("/athlete")
    public AjaxResult getAthlete(@Param("athlete_omit") athlete_omit athlete_omit){
        athlete_omit athleteOmitDto = skeetService.getathleteById(athlete_omit.getId());
        LocalDate currentDate = LocalDate.now();
        if (!Objects.isNull(athleteOmitDto.getAge())){
            LocalDate birthDate = Instant.ofEpochSecond(athleteOmitDto.getAge())
                    .atZone(ZoneId.systemDefault())
                    .toLocalDate();
            long age = ChronoUnit.YEARS.between(birthDate, currentDate);
            athleteOmitDto.setAge(age);
        }
        if (!Objects.isNull(athleteOmitDto.getStart())){
            LocalDate startDate = Instant.ofEpochSecond(athleteOmitDto.getStart())
                    .atZone(ZoneId.systemDefault())
                    .toLocalDate();
            long start = ChronoUnit.YEARS.between(startDate, currentDate);
            athleteOmitDto.setStart(start);
        }
        return AjaxResult.success(athleteOmitDto);
    }

    @GetMapping("/match")
    public AjaxResult getAthleteMatch(@Param("athlete_omit") athlete_omit athlete_omit){
        return AjaxResult.success(skeetService.getMatch(athlete_omit.getId()));
    }
}
